#!/bin/bash -v
#BSUB -o OUTPUT_FILES/%J.o
#BSUB -a mpich_gm
#BSUB -J specfem_kernel

# this is kernel simulation script
# Qinya Liu, May 2007, Caltech

BASEMPIDIR=/scratch/$USER/DATABASES_MPI

echo "$LSB_MCPU_HOSTS" > OUTPUT_FILES/lsf_machines
echo "$LSB_JOBID" > OUTPUT_FILES/jobid

remap_lsf_machines.pl OUTPUT_FILES/lsf_machines > OUTPUT_FILES/machines

shmux -M50 -Sall -c "mkdir -p /scratch/$USER/; mkdir -p $BASEMPIDIR.$LSB_JOBID" - < OUTPUT_FILES/machines >/dev/null

# main parameter file
sed -e "s:^LOCAL_PATH .*:LOCAL_PATH                      =  $BASEMPIDIR.$LSB_JOBID:" < DATA/Par_file > Par_file.tmp
mv Par_file.tmp DATA/Par_file

# mesher parameter file
sed -e "s:^LOCAL_PATH .*:LOCAL_PATH                      =  $BASEMPIDIR.$LSB_JOBID:" < DATA/meshfem3D_files/Mesh_Par_file > Par_file.tmp
mv Par_file.tmp DATA/meshfem3D_files/Mesh_Par_file

current_pwd=$PWD

change_simulation_type.pl -F
cd bin/
mpirun.lsf --gm-no-shmem --gm-copy-env ./xmeshfem3D
mpirun.lsf --gm-no-shmem --gm-copy-env ./xgenerate_databases
mpirun.lsf --gm-no-shmem --gm-copy-env ./xspecfem3D


#### this is the part needs to be revised for each kernel simulation #####
mkdir -p $current_pwd/OUTPUT_FILES/SEM
cd $current_pwd/OUTPUT_FILES/SEM
collect_seismo_lsf_multi.pl $current_pwd/OUTPUT_FILES/lsf_machines $current_pwd/DATA/Par_file
#xcut_velocity 29.0 32.5 3 GSC*.semd
xcreate_adjsrc_traveltime 29.0 32.5 3 GSC*.semd
rename .semd.adj .adj *.semd.ad
cd $current_pwd
##########################################################################

change_simulation_type.pl -b
cd bin/
mpirun.lsf --gm-no-shmem --gm-copy-env ./xspecfem3D

shmux -M50 -Sall -c "cp $BASEMPIDIR.$LSB_JOBID/*alpha_kernel.bin $current_pwd/MESH" - < OUTPUT_FILES/machines >/dev/null

